case class homework3(username: String, location: String, startTime: Int, duration: Int)

object Third extends App {
  // 将数据封装到样例类中，初始化数据到集合
  val homework3Lst: List[homework3] = List(
    homework3("userA", "locationA", 8, 60),
    homework3("userA", "locationA", 9, 60),
    homework3("userB", "locationB", 11, 80),
    homework3("userB", "locationB", 10, 60)
  )

  // 分组，key为姓名加位置，确定唯一，算子为groupBy
  // 分组，key为姓名加位置，确定唯一，算子为groupBy
  val grouphomework3: Map[String, List[homework3]] = homework3Lst.groupBy(user => user.username + "-" + user.location)
  println("分组")
  grouphomework3.foreach {
    case (k, v) => println(s"key:$k, value:$v")
  }

  // 排序，根据开始时间顺序,算子为mapValues
  val sorthomework3: Map[String, List[homework3]] = grouphomework3.mapValues(_.sortBy(_.startTime))
  println("排序")
  sorthomework3.foreach {
    case (k, v) => println(s"key:$k, value:$v")
  }


  // 统计,算子为mapValues，最终计算出不同组里的连续的开始时间的最终停留时长，原始数据不存在不连续，不考虑不连续
  var firstTime = 0
  val sumhomework3: Map[String, Int] = sorthomework3.mapValues(lst => {
    // 返回开始时间
    firstTime = lst.head.startTime
    // 返回总停留时长
    lst.map(_.duration).sum
  })
  println("最终")
  // 输出最终结果
  sumhomework3.foreach {
    case (k, v) => println(s"key:$k, 开始时间：$firstTime, value:$v")
  }

}